package com.twu.bm.football;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.*;

public class FootballLeagueApp {

    private BufferedReader reader;
    private Map<String, MenuOption> menu;
    private PrintStream printStream;
    private String chooseMessage = "Enter a number to select an option:";
    private String listLeagueMessage = "1 - List players and coaches";
    private String playerSearchMessage = "2 - Search for a player";
    private String teamListMessage = "3 - See members of team";

    public FootballLeagueApp(PrintStream printStream, BufferedReader reader, Map<String, MenuOption> menu) {
        this.printStream = printStream;
        this.reader = reader;
        this.menu = menu;
    }

    public static void main (String[] args) throws IOException {
        PrintStream printStream = System.out;
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        Set<Player> playersList = createPlayerList();
        Set<Coach> coachesList = createCoachList();
        League league = new League(playersList, coachesList);

        Map<String, MenuOption> menu = createMenu(printStream, reader, league);

        new FootballLeagueApp(printStream, reader, menu).run();
    }

    private static Set<Coach> createCoachList() {
        Set<Coach> coachesList = new HashSet<Coach>();
        coachesList.add(new Coach("Joe Black", "Bengals", "Assistant Coach"));
        return coachesList;
    }

    private static Set<Player> createPlayerList() {
        Set<Player> playerList = new HashSet<Player>();
        playerList.add(new Player("John Smith", 30, "Bengals", 13));
        playerList.add(new Player("Alex White", 29, "Ravens", 26));

        return playerList;
    }

    private static Map<String, MenuOption> createMenu(PrintStream printStream, BufferedReader reader, League league) {
        Map<String, MenuOption> options = new HashMap<String, MenuOption>();
        options.put("1", new ListOption(printStream, league));
        options.put("2", new SearchOption(printStream, reader, league));
        options.put("3", new TeamListOption(printStream, reader));

        return options;
    }

    public void run() throws IOException {
        printMenu();

        String input = reader.readLine();
        if (menu.containsKey(input))
            menu.get(input).performAction();
        else
            printStream.println("Invalid input!");
    }

    private void printMenu() {
        printStream.println(chooseMessage);
        printStream.println(listLeagueMessage);
        printStream.println(playerSearchMessage);
        printStream.println(teamListMessage);
    }
}
